package com.mz.mattress.admin.api.merchant;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mz.mattress.admin.api.req.DeviceQueryReq;
import com.mz.mattress.admin.api.resp.DeviceSum;
import com.mz.mattress.admin.common.Holder;
import com.mz.mattress.admin.service.DeviceService;
import com.mz.mattress.api.common.Constants;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Device;
import com.mz.mattress.api.jwt.MerchantJwt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping(Constants.MERCHANT_PATH + "device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @GetMapping("/listPage")
    public Result<IPage<Device>> listPage(DeviceQueryReq req) {

        Integer type = req.getType();

        Integer typeId = req.getTypeId();
        if (typeId.equals(Constants.TYPE_ID_NOT_FIND)) {
            return Result.ok();
        }

        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        if (type.equals(Constants.ROLE_MERCHANT)) {
            if (typeId.equals(Constants.TYPE_ID_ALL)) {
                req.setMemberId(userJwt.getMemberMerId());
            } else {
                req.setMerchantId(typeId);
            }
        } else if (type.equals(Constants.ROLE_SALE)) {
            req.setSaleId(typeId);
            req.setTopSaleId(typeId);
        }

        return deviceService.listPageMerchant(req);
    }

    @GetMapping(value = "/{id}")
    public Result<Device> detail(@PathVariable Integer id) {
        return deviceService.getById(id);
    }

    @GetMapping(value = "/list")
    public Result<List<Device>> list(@RequestParam Integer merchantId) {
        return deviceService.list(merchantId);
    }

    @GetMapping(value = "/deviceSum")
    public Result<DeviceSum> deviceSum(@RequestParam Integer typeId) {
        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        return deviceService.deviceSum(typeId, userJwt.getMemberMerId());
    }

}
